// components/search/index.js
import {KeywordModel} from 'keyword.js'
import {keywordll} from '../../model/keyword'
import {getList} from 'getList.js'

const bookList = new getList()
const keyword = new KeywordModel()
const keywords = new keywordll()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    historyKeys: {
      type: Array
    },
    hotKeys: {
      type: Array
    },
    a:{
      type: String
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    keywords: "",
    keywordClear: '',
    bookNames: [],
    searchList:[],
    showList:false,
    showHistory:true
  },
  attached: function() {
    console.log(this.data.historyKeys)
    const keywordl = keywords.getKeyword()
    keywordl.then(
      (res) => {
        for (let i = 0; i < res.data.length; i++) {
          this.data.bookNames.unshift(res.data[i].bookname)
        }
        console.log(keyword.getHistory())
        this.setData({
          hotKeys: this.data.bookNames,
          historyKeys: keyword.getHistory()
        })
        console.log(this.data.hotkeys)
      })
  },
  /**
   * 组件的方法列表
   */
  methods: {
    showInput: function(e) {
      this.data.keywords = e.detail.value
      // console.log(this.data.keywords)
      const list = bookList.getBookList(this.data.keywords);
      list.then(
        res => {
          if (this.data.keywords){
          this.setData({
            searchList: res.data,
            showHistory:false,
            showList:true
          })
          }else{
            this.setData({
              showHistory: true,
              showList: false
            })
          }
          console.log(res.data)
        }
      )
    },
    onConfirm: function() {
      keyword.addToHistory(this.data.keywords)
      // console.log(this.data.keywords)
      this.triggerEvent('getKeyword', { keywordValue: this.data.keywords})
    },
    onBindblur: function(e) {
      this.data.keywordClear = e.detail
    },
    closeSearch: function() {
      this.triggerEvent('onCloseSearch', {})
    },
    onGetKeyword:function(e){
      console.log(e.detail.comment)
      this.triggerEvent('getTag', { keyword:e.detail.comment })
    },
    getSearchList:function(e){
      console.log(e.detail.listName)
      this.triggerEvent('onGetSearchList', {listName: e.detail.listName})
    }
  }
})